package com.ethink.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ethink.system.api.request.SystemRoleAddRequest;
import com.ethink.system.api.request.SystemRoleMenuAddRequest;
import com.ethink.system.api.request.SystemRoleMenuUpdateRequest;
import com.ethink.system.common.entity.SystemRoleMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ethink.system.common.mapper.SystemRoleMenuMapper;
import com.ethink.system.converter.SystemRoleMenuConverter;
import com.ethink.system.service.SystemRoleMenuService;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统角色菜单关联表 Service接口实现
 *
 * @author default
 * @date 2022-09-27
 */
@Slf4j
@Service
public class SystemRoleMenuServiceImpl extends ServiceImpl<SystemRoleMenuMapper, SystemRoleMenu> implements SystemRoleMenuService {

    @Autowired
    private SystemRoleMenuMapper systemRoleMenuMapper;

    @Autowired
    private SystemRoleMenuConverter systemRoleMenuConverter;


    @Override
    public void saveBatch(SystemRoleMenuAddRequest request) {
        if (request.getMenuIdList().size() == 0){
            return;
        }
        List<SystemRoleMenu> list = new ArrayList<>();
        SystemRoleMenu systemRoleMenu;
        for (Long aLong : request.getMenuIdList()) {
            systemRoleMenu = new SystemRoleMenu();
            systemRoleMenu.setMenuId(aLong);
            systemRoleMenu.setRoleId(request.getRoleId());
            list.add(systemRoleMenu);
        }
        saveBatch(list);
    }

    @Override
    public void updateBatch(SystemRoleMenuUpdateRequest request) {
        deleteByRoleId(request.getRoleId());
        SystemRoleMenuAddRequest systemRoleMenuAddRequest = BeanUtil.copyProperties(request, SystemRoleMenuAddRequest.class);
        saveBatch(systemRoleMenuAddRequest);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<SystemRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRoleMenu::getRoleId,roleId);
        systemRoleMenuMapper.delete(wrapper);
    }
}
